package com.cw.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cw.dao.GenericDao;
import com.cw.po.Income;
import com.cw.service.IncomeService;

@Service("incomeService")
public class IncomeServiceImpl extends BaseServiceImpl<Income> implements
	IncomeService {

	private GenericDao<Income> incomeDao;
	
	@Override
	@Resource(name = "incomeDao")
	public void setDao(GenericDao<Income> dao) {
		// TODO Auto-generated method stub
		super.setDao(dao);
		this.incomeDao = dao;
	}

	@Override
	public List<Income> findMyIncomes(Integer userid) {
		String hql = "from Income s where s.user.id = ?";
		return incomeDao.findEntityBySql(hql, userid);
	}

	@Override
	public Income getIncome(Integer sId) {
		// TODO Auto-generated method stub
		return incomeDao.getEntity(sId);
	}

	@Override
	public void deleteIncome(Integer sId) {
		String incomeHql = "delete from Income s where s.id = ?" ;
		incomeDao.batchHandlerByHQL(incomeHql, sId);		
	}

	@Override
	public List getTotals(Integer userid) {
		String hql = "select " +
				" sum(wage),sum(expenseacc),sum(others) from Income s where s.user.id = ?";
		List<Object> totalLst = incomeDao.findPropBySql(hql, userid);
		// TODO Auto-generated method stub
		if(totalLst.size()>0)
		{
			Object[] val = (Object[])totalLst.get(0);
//			Long[] result = new Long[val.length+1];
			List<Object> arrs = new ArrayList<Object>();
			Long sum = 0l;
			for(Object v:val){
				if(v==null){
					return null;
				}
				arrs.add(v);
				sum += (Long)v;
			}
			arrs.add(sum);
			return arrs;
		}
		return null;
	}

	@Override
	public void updateIncome(Income income) {
		incomeDao.updateEntity(income);
	}
}
